{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]14499/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","excerpt":"문제 크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 이 지도의 위에 주사위가 하나 놓여져 있으며, 주사위의 전개도는 아래와 같다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다.    2\n4 1 3\n  5\n  6 주사위는 지도 위에 윗 면이 1이고, 동쪽을 …","html":"<h2>문제</h2>\n<p>크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 이 지도의 위에 주사위가 하나 놓여져 있으며, 주사위의 전개도는 아래와 같다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. </p>\n<p>  2\n4 1 3\n5\n6</p>\n<p>주사위는 지도 위에 윗 면이 1이고, 동쪽을 바라보는 방향이 3인 상태로 놓여져 있으며, 놓여져 있는 곳의 좌표는 (x, y) 이다. 가장 처음에 주사위에는 모든 면에 0이 적혀져 있다.</p>\n<p>지도의 각 칸에는 정수가 하나씩 쓰여져 있다. 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다. 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.</p>\n<p>주사위를 놓은 곳의 좌표와 이동시키는 명령이 주어졌을 때, 주사위가 이동했을 때 마다 상단에 쓰여 있는 값을 구하는 프로그램을 작성하시오.</p>\n<p>주사위는 지도의 바깥으로 이동시킬 수 없다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.</p>\n<h2>입, 출력</h2>\n<p>입력</p>\n<ul>\n<li>첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다.</li>\n<li>둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 주사위를 놓은 칸에 쓰여 있는 수는 항상 0이다. 지도의 각 칸에 쓰여 있는 수는 10 미만의 자연수 또는 0이다.</li>\n<li>마지막 줄에는 이동하는 명령이 순서대로 주어진다. 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4로 주어진다.</li>\n</ul>\n<p>출력</p>\n<ul>\n<li>이동할 때마다 주사위의 윗 면에 쓰여 있는 수를 출력한다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Dice {\n    int[][] map;\n    int[] commands;\n    int[] numbers = new int[6];\n    int[] rangeX = { 0, 0, -1, 1 };\n    int[] rangeY = { 1, -1, 0, 0 };\n    int N, M, K;\n    int top = 0, front = 4, east = 2;\n    ...\n\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>주사위와 지도의 정보를 저장하는 클래스</li>\n<li>top, front, east 정보를 계속 변경하는 방식으로 주사위의 숫자 정보를 추적한다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">public void game(int x, int y) {\n    for (int i = 0; i &lt; K; i++) {\n        int nx = x + rangeX[commands[i] - 1];\n        int ny = y + rangeY[commands[i] - 1];\n\n        if (isInTheMap(nx, ny)) continue;\n\n        x = nx;\n        y = ny;\n\n        rollDice(commands[i] - 1);\n\n        if (map[x][y] == 0) {\n            map[x][y] = numbers[5 - top];\n        } else {\n            numbers[5 - top] = map[x][y];\n            map[x][y] = 0;\n        }\n\n        System.out.println(numbers[top]);\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>\n<p>명령어에 따라 지도에서의 주사위 위치를 변경시킨다.</p>\n<ul>\n<li>지도 범위 안인 경우만 주사위 정보를 수정하고, 상단의 숫자를 출력한다.</li>\n</ul>\n</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">private void rollDice(int command) {\n    int t, e, f;\n    t = top;\n    e = east;\n    f = front;\n    switch (command) {\n        case 0:\n            top = 5 - e;\n            east = t;\n            break;\n        case 1:\n            top = e;\n            east = 5 - t;\n            break;\n        case 2:\n            top = f;\n            front = 5 - t;\n            break;\n        case 3:\n            top = 5 - f;\n            front = t;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>주사위의 top, front, east 정보를 업데이트하는 메소드</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.Arrays;\n\nclass Dice {\n    int[][] map;\n    int[] commands;\n    int[] numbers = new int[6];\n    int[] rangeX = { 0, 0, -1, 1 };\n    int[] rangeY = { 1, -1, 0, 0 };\n    int N, M, K;\n    int top = 0, front = 4, east = 2;\n\n    public Dice(int N, int M, int K) {\n        this.N = N;\n        this.M = M;\n        this.K = K;\n        map = new int[N][M];\n        commands = new int[K];\n    }\n\n    public void game(int x, int y) {\n        for (int i = 0; i &lt; K; i++) {\n            int nx = x + rangeX[commands[i] - 1];\n            int ny = y + rangeY[commands[i] - 1];\n\n            if (isInTheMap(nx, ny)) continue;\n\n            x = nx;\n            y = ny;\n\n            rollDice(commands[i] - 1);\n\n            if (map[x][y] == 0) {\n                map[x][y] = numbers[5 - top];\n            } else {\n                numbers[5 - top] = map[x][y];\n                map[x][y] = 0;\n            }\n\n            System.out.println(numbers[top]);\n        }\n    }\n\n    private boolean isInTheMap(int x, int y) {\n        return (x &lt; 0 | x &gt;= N | y &lt; 0 | y &gt;= M);\n    }\n\n    private void rollDice(int command) {\n        int t, e, f;\n        t = top;\n        e = east;\n        f = front;\n        switch (command) {\n            case 0:\n                top = 5 - e;\n                east = t;\n                break;\n            case 1:\n                top = e;\n                east = 5 - t;\n                break;\n            case 2:\n                top = f;\n                front = 5 - t;\n                break;\n            case 3:\n                top = 5 - f;\n                front = t;\n        }\n    }\n}\n\npublic class Main {\n\n    public static void main(String[] args) throws IOException {\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n        int[] line = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n        int N = line[0], M = line[1], x = line[2], y = line[3], K = line[4];\n\n        Dice dice = new Dice(N, M, K);\n\n        for (int i = 0; i &lt; N; i++) dice.map[i] = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n\n        dice.commands = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n\n        dice.game(x, y);\n\n        br.close();\n    }\n}</code>\n        </deckgo-highlight-code>\n  \t</div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>\n<p>다른 구현은 모두 쉬웠는데, <code class=\"language-text\">rollDice()</code> 구현이 제일 어려웠다.</p>\n<ul>\n<li>원래는 명령어가 주어지면, 전역으로 가지고 있는 bottom의 정보를 업데이트하는 방식을 사용하려 했는데, 아무리 생각해도 안되는 경우가 많은 것 같아 게시판을 참고했다.</li>\n</ul>\n</li>\n</ul>\n<h3>📕 출처</h3>\n<p>Baekjoon : <a href=\"https://www.acmicpc.net/problem/14499\">https://www.acmicpc.net/problem/14499</a></p>","frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기","date":"March 28, 2022","update":"March 28, 2022","tags":["구현","시뮬레이션"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]14499/","readingTime":{"minutes":6.93}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[Algorithm]알고리즘분석/"},"frontmatter":{"title":"💡 알고리즘 분석"}},"next":{"fields":{"slug":"/[JPA]10/"},"frontmatter":{"title":"📚 10. 객체지향 쿼리 언어"}}},"pageContext":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","series":"👩‍💻 BOJ","previousPostId":"b783c57f-f958-5c14-9935-06b16995e8f1","nextPostId":"a41dc548-d976-533a-a680-96e86a627d35"}},"staticQueryHashes":[]}